﻿using System;
using System.Net;

namespace Tequila.Phone.Sharing
{
    /// <summary>
    /// Base implementation of <see cref="IShareService">IShareService</see>
    /// </summary>
    public abstract class BaseShareService : IShareService
    {
        #region Implementation of IShareService

        /// <summary>
        /// Occurs when the post was successful.
        /// </summary>
        public event EventHandler<EventArgs> PostSuccess;

        /// <summary>
        /// Occurs when the post failed.
        /// </summary>
        public event EventHandler<ErrorEventArgs> PostError;

        /// <summary>
        /// Occurs when login was successful.
        /// </summary>
        public event EventHandler<EventArgs> LoginSuccess;

        /// <summary>
        /// Occurs when login failed.
        /// </summary>
        public event EventHandler<ErrorEventArgs> LoginError;

        /// <summary>
        /// Occurs when initialization was successful.
        /// </summary>
        public event EventHandler<EventArgs> InitSuccess;

        /// <summary>
        /// Occurs when initialization failed.
        /// </summary>
        public event EventHandler<ErrorEventArgs> InitError;

        /// <summary>
        /// Occurs when connection was not successful.
        /// </summary>
        public event EventHandler<ErrorEventArgs> ConnectionError;

        /// <summary>
        /// Determines whether authentication performed or not.
        /// </summary>
        /// <returns></returns>
        public virtual bool IsAuthenticated()
        {
            return true;
        }

        /// <summary>
        /// Performs initialization.
        /// </summary>
        public virtual void Init()
        {
            // bypass
            OnInitSuccess();
        }

        /// <summary>
        /// When implemented, shares <see cref="Article">article</see>.
        /// </summary>
        /// <param name="article"></param>
        public abstract void Post(Article article);

        /// <summary>
        /// When implemented, shares <see cref="GaleryImage">image</see>.
        /// </summary>
        /// <param name="image"></param>
        public abstract void PostImage(GaleryImage image);

        /// <summary>
        /// When implemented, returns OAUTH login <see cref="T:System.Uri">uri</see>.
        /// </summary>
        /// <returns>
        /// Type: <see cref="T:System.Uri"/>.
        /// </returns>
        public virtual Uri GetOAuthLoginUri()
        {
            throw new NotSupportedException();
        }

        /// <summary>
        /// When implemented, returns logout url.
        /// </summary>
        /// <returns>
        /// Type: <see cref="T:System.Uri"/>.
        /// </returns>
        public abstract Uri GetLogoutUrl();

        /// <summary>
        /// When implemented, processes OAUTH output requested by <see cref="T:System.Uri">uri</see>.
        /// </summary>
        public virtual void ProcessOAuthBrowserResult(Uri openedUri)
        {
            throw new NotSupportedException();
        }

        /// <summary>
        /// When implemented, creates custom Login uri.
        /// </summary>
        public abstract void BuildLoginUri();

        /// <summary>
        /// Gets or sets Current User ID.
        /// </summary>
        public long CurrentUserId { get; set; }

        #endregion

        /// <summary>
        /// Triggers InitSuccess event.
        /// </summary>
        protected void OnInitSuccess()
        {
            InitSuccess.Fire(this, EventArgs.Empty);
        }

        /// <summary>
        /// Triggers InitError event.
        /// </summary>
        /// <param name="error"></param>
        protected void OnInitError(string error)
        {
            InitError.Fire(this, new ErrorEventArgs {ErrorMessage = error});
        }

        /// <summary>
        /// Triggers ConnectionError event.
        /// </summary>
        /// <param name="exception"></param>
        protected void OnConnectionError(WebException exception)
        {
            ConnectionError.Fire(this, new ErrorEventArgs {ErrorMessage = exception.Message});
        }

        /// <summary>
        /// Triggers LoginSuccess event.
        /// </summary>
        protected void OnLoginSuccess()
        {
            LoginSuccess.Fire(this, EventArgs.Empty);
        }

        /// <summary>
        /// Triggers LoginError event.
        /// </summary>
        protected void OnLoginError(string error)
        {
            LoginError.Fire(this, new ErrorEventArgs {ErrorMessage = error});
        }

        /// <summary>
        /// Triggers PostSuccess event.
        /// </summary>
        protected void OnPostSuccess()
        {
            PostSuccess.Fire(this, EventArgs.Empty);
        }

        /// <summary>
        /// Triggers PostError event.
        /// </summary>
        protected void OnPostError(string error)
        {
            PostError.Fire(this, new ErrorEventArgs { ErrorMessage = error });
        }
    }
}
